clear all
*cap log close
set more off
set seed 603


// compute p-value of tests of equality -------------------------------

import delimited using "${est}/exper_q2.csv", clear
keep if (par=="d0"|par=="d1")
gen  pval = 2 * (1 - normal(abs(est / se)))
qui summ pval if yvar == "cite_ny1" & par == "d0"
di "recid, poly, y0: pval = `r(mean)'"
qui summ pval if yvar == "cite_ny1" & par == "d1"
di "recid, poly, y1: pval = `r(mean)'"
qui summ pval if yvar == "contest" & par == "d0"
di "contest, poly, y0: pval = `r(mean)'"
qui summ pval if yvar == "contest" & par == "d1"
di "contest, poly, y1: pval = `r(mean)'"

import delimited using "${est}/exper_b5.csv", clear
keep if (par=="d0"|par=="d1")
gen  pval = 2 * (1 - normal(abs(est / se)))
qui summ pval if yvar == "cite_ny1" & par == "d0"
di "recid, bins, y0: pval = `r(mean)'"
qui summ pval if yvar == "cite_ny1" & par == "d1"
di "recid, bins, y1: pval = `r(mean)'"
qui summ pval if yvar == "contest" & par == "d0"
di "contest, bins, y0: pval = `r(mean)'"
qui summ pval if yvar == "contest" & par == "d1"
di "contest, bins, y1: pval = `r(mean)'"




// panel a: binscatter for recidivism -------------------------------------
// read ests --------------
import delimited using "${est}/exper_q2.csv", clear
keep if yvar == "cite_ny1"

* store polynomials -------
forval g=0/1 {
	qui summ est if g==`g' & par=="y0"
	local beta0_`g' = `r(mean)'
	forval q=1/2 {
		qui summ est if g==`g' & par=="beta`q'"
		local beta`q'_`g' = `r(mean)'
	}
}

* store extrapolated ------------
keep if par == "y0"|par=="y1"
gen Z = .
replace Z = 0 if par=="y0"
replace Z = 1 if par=="y1"
gen est1 = est 
gen ub1  = ub
gen lb1  = lb
keep  yvar g Z est ub lb 
order yvar g Z est ub lb 
tempfile extrap
save    `extrap'
// ---------------------------------


// read binscatter -----------------
use "${temp}/exper_binscatter", clear 
keep if yvar == "cite_ny1"
rename dots_x Z
rename dots_fit est 
qui append using `extrap', gen(flag)
sort yvar g Z 


// superimpose quadratic fit ----------
gen fit = .
forval g=0/1 {
	replace fit = `beta0_`g'' + `beta1_`g''*Z + `beta2_`g''*Z^2 if g==`g'
}

// Make figure --------
#delimit ;
scatter est Z if g==0, msymbol(Oh) mcolor(dkgreen%95) msize(medium) || 
scatter est Z if g==1, msymbol(Sh) mcolor(purple%95) msize(medium) ||
line fit Z if g==0, lcolor(dkgreen) lpattern(dash) ||
line fit Z if g==1, lcolor(purple) lpattern(dash) ||
rspike ub lb Z if g==0 & flag==1, lcolor(dkgreen) ||
scatter est Z if g==0 & flag==1, msymbol(O) mcolor(dkgreen) ||
rspike ub lb Z if g==1 & flag==1, lcolor(purple) ||
scatter est Z if g==1 & flag==1, msymbol(S) mcolor(purple) 
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
ylab(0.33(0.01)0.38,nogrid) xlab(,nogrid)
xtitle("Officer Stringency") ytitle("Pr(Reoffend)")
legend(ring(0) cols(1) pos(2) order(1 2) 
lab(1 "Low Experience") lab(2 "High Experience")
region(lstyle(border)))  ;
#delimit cr 
graph export "${out}/apx_extrap/exper_extrap_recid.pdf", replace 
// -------------------------------------------------------------------------





// panel b: binscatter for contesting -----------------------------------
// read ests --------------
import delimited using "${est}/exper_q2.csv", clear
keep if yvar == "contest"

* store polynomials -------
forval g=0/1 {
	qui summ est if g==`g' & par=="y0"
	local beta0_`g' = `r(mean)'
	forval q=1/2 {
		qui summ est if g==`g' & par=="beta`q'"
		local beta`q'_`g' = `r(mean)'
	}
}

* store extrapolated ------------
keep if par == "y0"|par=="y1"
gen Z = .
replace Z = 0 if par=="y0"
replace Z = 1 if par=="y1"
gen est1 = est 
gen ub1  = ub
gen lb1  = lb
keep  yvar g Z est ub lb 
order yvar g Z est ub lb 
tempfile extrap
save    `extrap'
// ---------------------------------


// read binscatter -----------------
use "${temp}/exper_binscatter", clear 
keep if yvar == "contest"
rename dots_x Z
rename dots_fit est 
qui append using `extrap', gen(flag)
sort yvar g Z 


// superimpose quadratic fit ----------
gen fit = .
forval g=0/1 {
	replace fit = `beta0_`g'' + `beta1_`g''*Z + `beta2_`g''*Z^2 if g==`g'
}

// Make figure --------
#delimit ;
scatter est Z if g==0, msymbol(Oh) mcolor(dkgreen%95) msize(medium) || 
scatter est Z if g==1, msymbol(Sh) mcolor(purple%95) msize(medium) ||
line fit Z if g==0, lcolor(dkgreen) lpattern(dash) ||
line fit Z if g==1, lcolor(purple) lpattern(dash) ||
rspike ub lb Z if g==0 & flag==1, lcolor(dkgreen) ||
scatter est Z if g==0 & flag==1, msymbol(O) mcolor(dkgreen) ||
rspike ub lb Z if g==1 & flag==1, lcolor(purple) ||
scatter est Z if g==1 & flag==1, msymbol(S) mcolor(purple) 
graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
ylab(0.2(0.05)0.35,nogrid) xlab(,nogrid)
xtitle("Officer Stringency") ytitle("Pr(Reoffend)")
legend(ring(0) cols(1) pos(10) order(1 2) 
lab(1 "Low Experience") lab(2 "High Experience")
region(lstyle(border)))  ;
#delimit cr 
graph export "${out}/apx_extrap/exper_extrap_contest.pdf", replace 
// -----------------------------------------------------------------------







// -----------------------------------------------------------------------
// Levels and gains for recidivism
forval g = 0/1 {
	
	import delimited using "${est}/exper_q2.csv", clear
	keep if yvar == "cite_ny1"
	replace par = trim(upper(par))
	keep if g == `g'

	* populate levels and gains ----------------
	do "${code}/utility/create_levels_gains.do"

	drop if mi(y0)
	keep ind y0 y0u y0l y1 y1u y1l delta deltau deltal
	replace ind = 0.2 if _n == 1
	replace ind = 0.5  if _n == 2 
	replace ind = 0.8 if _n == 3

	gen G = `g'
	*gen G = `g'-1
	tempfile TEMP`g'
	save    `TEMP`g''
	
}


use `TEMP0', clear
qui append using `TEMP1'
replace ind = ind-0.02 if G==0
replace ind = ind+0.02 if G==1


#delimit ; 
twoway 
(rbar y1u y1l ind if G==0, color(magenta%20) barwidth(0.02))
(scatter y0 ind if G==0, msize(medium) msymbol(Dh) mcolor(dkgreen))
(pcarrow y0 ind y1 ind if G==0, lcolor(maroon) lwidth(medthick) mcolor(maroon) barbsize(medlarge))
(rbar y1u y1l ind if G==1, color(dkorange%20) barwidth(0.02))
(scatter y0 ind if G==1, msize(large) msymbol(Sh) mcolor(purple))
(pcarrow y0 ind y1 ind if G==1, lcolor(dkorange) lwidth(medthick) mcolor(dkorange) barbsize(medlarge)),
xscale(r(0 1))
xlabel(0.2 "All Motorists" 0.5 "Treated" 0.8 "Untreated",nogrid) ylab(,nogrid) 
legend(ring(0) pos(8) order(2 5) cols(1) 
label(2 "Low Experience") label(5 "High Experience")
region(lstyle(border))) 
xtitle("") ytitle("Pr(Reoffend)") 
graphregion(color(white)) bgcolor(white) plotregion(lcolor(black) lwidth(medthin)) ;
#delimit cr
graph export "${out}/apx_extrap/exper_est_recid.pdf", replace 
// -----------------------------------------------------------------------




// -----------------------------------------------------------------------
// Levels and gains for court contesting
forval g = 0/1 {
	
	import delimited using "${est}/exper_q2.csv", clear
	keep if yvar == "contest"
	replace par = trim(upper(par))
	keep if g == `g'

	* populate levels and gains ----------------
	do "${code}/utility/create_levels_gains.do"

	drop if mi(y0)
	keep ind y0 y0u y0l y1 y1u y1l delta deltau deltal
	replace ind = 0.2 if _n == 1
	replace ind = 0.5  if _n == 2 
	replace ind = 0.8 if _n == 3

	gen G = `g'
	*gen G = `g'-1
	tempfile TEMP`g'
	save    `TEMP`g''
	
}


use `TEMP0', clear
qui append using `TEMP1'
replace ind = ind-0.02 if G==0
replace ind = ind+0.02 if G==1


#delimit ; 
twoway 
(rbar y1u y1l ind if G==0, color(magenta%20) barwidth(0.02))
(scatter y0 ind if G==0, msize(medium) msymbol(Dh) mcolor(dkgreen))
(pcarrow y0 ind y1 ind if G==0, lcolor(maroon) lwidth(medthick) mcolor(maroon) barbsize(medlarge))
(rbar y1u y1l ind if G==1, color(dkorange%20) barwidth(0.02))
(scatter y0 ind if G==1, msize(large) msymbol(Sh) mcolor(purple))
(pcarrow y0 ind y1 ind if G==1, lcolor(dkorange) lwidth(medthick) mcolor(dkorange) barbsize(medlarge)),
xscale(r(0 1))
xlabel(0.2 "All Motorists" 0.5 "Treated" 0.8 "Untreated",nogrid) ylab(,nogrid) 
legend(ring(0) pos(8) order(2 5) cols(1) 
label(2 "Low Experience") label(5 "High Experience")
region(lstyle(border))) 
xtitle("") ytitle("Pr(Reoffend)") 
graphregion(color(white)) bgcolor(white) plotregion(lcolor(black) lwidth(medthin)) ;
#delimit cr
graph export "${out}/apx_extrap/exper_est_contest.pdf", replace 
// -----------------------------------------------------------------------


